#include <LCD.H>

void delay(uint n)
{
   uint i, j;
   for (i = 0; i < n; i++)
      for (j = 0; j < 123; j++)
         ;
}

void delay5(uchar n)
{
   do
   {
      _nop_();
      _nop_();
      _nop_();
      n--;
   } while (n);
}

void check_busy(void)
{
   uchar dt;
   do
   {
      dt = 0xff;
      en = 0;
      rs = 0;
      rw = 1;
      en = 1;
      dt = P0;
   } while (dt & 0x80);
   en = 0;
}

void write_command(uchar com)
{
   check_busy();
   en = 0;
   rs = 0;
   rw = 0;
   P0 = com;
   en = 1;
   _nop_();
   en = 0;
   delay(1);
}

void lcd_initial(void)
{
   write_command(0x38);
   write_command(0x0c);
   write_command(0x06);
   write_command(0x01);
   delay(1);
}

void write_data(uchar dat)
{
   check_busy();
   en = 0;
   rs = 1;
   rw = 0;
   P0 = dat;
   en = 1;
   _nop_();
   en = 0;
   delay(1);
}
void string(uchar position, uchar *s)
{
   write_command(position);
   while (*s > 0)
   {
      write_data(*s++);
      delay(1);
   }
}